<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>留言板系统 - 管理员登录</title>
    <style>
        :root {
            --primary-color: #4285f4;
            --error-color: #d32f2f;
            --text-color: #333;
            --light-text: #555;
            --border-color: #ddd;
            --background-color: #f5f5f5;
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: var(--background-color);
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .login-card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            width: 100%;
            max-width: 400px;
            padding: 40px;
            animation: fadeIn 0.5s ease;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        .login-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .login-header h1 {
            color: var(--text-color);
            font-size: 24px;
            font-weight: 600;
            margin-bottom: 8px;
        }

        .login-header p {
            color: var(--light-text);
            font-size: 14px;
        }

        .login-form .form-group {
            margin-bottom: 20px;
        }

        .login-form label {
            display: block;
            margin-bottom: 8px;
            color: var(--text-color);
            font-size: 14px;
            font-weight: 500;
        }

        .login-form input {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid var(--border-color);
            border-radius: 6px;
            font-size: 15px;
            transition: all 0.3s;
        }

        .login-form input:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(66, 133, 244, 0.2);
        }

        .error-message {
            color: var(--error-color);
            font-size: 13px;
            margin-top: 5px;
            display: none;
        }

        .login-button {
            width: 100%;
            padding: 12px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 15px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 10px;
        }

        .login-button:hover {
            background-color: #3367d6;
        }

        .login-footer {
            text-align: center;
            margin-top: 20px;
            font-size: 13px;
            color: var(--light-text);
        }

        .login-footer a {
            color: var(--primary-color);
            text-decoration: none;
            transition: color 0.2s;
        }

        .login-footer a:hover {
            text-decoration: underline;
        }

        .server-error {
            color: var(--error-color);
            background-color: rgba(211, 47, 47, 0.1);
            padding: 12px;
            border-radius: 6px;
            margin-bottom: 20px;
            font-size: 14px;
            display: none;
            text-align: center;
        }
    </style>
</head>
<body>
<div class="login-card">
    <div class="login-header">
        <h1>管理员登录</h1>
        <p>请输入您的管理员账号和密码</p>
    </div>

    <div id="serverError" class="server-error"></div>

    <form id="adminLoginForm" class="login-form">
        <div class="form-group">
            <label for="adminUsername">管理员账号</label>
            <input type="text" id="adminUsername" name="username" placeholder="输入管理员账号">
            <div id="usernameError" class="error-message">请输入有效的管理员账号</div>
        </div>

        <div class="form-group">
            <label for="adminPassword">密码</label>
            <input type="password" id="adminPassword" name="password" placeholder="输入密码">
            <div id="passwordError" class="error-message">密码长度至少6位</div>
        </div>

        <button type="submit" class="login-button">登 录</button>
    </form>

    <div class="login-footer">
        <a href="/">← 返回前台首页</a>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const loginForm = document.getElementById('adminLoginForm');
        const usernameInput = document.getElementById('adminUsername');
        const passwordInput = document.getElementById('adminPassword');
        const usernameError = document.getElementById('usernameError');
        const passwordError = document.getElementById('passwordError');
        const serverError = document.getElementById('serverError');

        // 从URL获取错误信息（例如登录失败重定向时）
        const urlParams = new URLSearchParams(window.location.search);
        const errorMessage = urlParams.get('error');

        if (errorMessage) {
            serverError.textContent = decodeURIComponent(errorMessage);
            serverError.style.display = 'block';
        }

        loginForm.addEventListener('submit', function(e) {
            e.preventDefault();

            // 重置错误状态
            usernameError.style.display = 'none';
            passwordError.style.display = 'none';
            serverError.style.display = 'none';

            let isValid = true;
            const username = usernameInput.value.trim();
            const password = passwordInput.value.trim();

            // 验证用户名
            if (username === '') {
                usernameError.style.display = 'block';
                usernameInput.focus();
                isValid = false;
            }

            // 验证密码
            if (password === '') {
                passwordError.textContent = '请输入密码';
                passwordError.style.display = 'block';
                if (isValid) passwordInput.focus();
                isValid = false;
            } else if (password.length < 6) {
                passwordError.textContent = '密码长度至少6位';
                passwordError.style.display = 'block';
                if (isValid) passwordInput.focus();
                isValid = false;
            }

            if (isValid) {
                // 这里可以添加AJAX提交或直接提交表单
                console.log('提交登录:', { username, password });

                // 实际项目中应该使用AJAX或直接提交到后端
                // fetch('/admin/login', {
                //     method: 'POST',
                //     headers: {
                //         'Content-Type': 'application/json',
                //     },
                //     body: JSON.stringify({
                //         username: username,
                //         password: password
                //     })
                // })
                // .then(response => response.json())
                // .then(data => {
                //     if (data.success) {
                //         window.location.href = '/admin/dashboard';
                //     } else {
                //         serverError.textContent = data.message || '登录失败';
                //         serverError.style.display = 'block';
                //     }
                // });

                // 示例中直接提交表单
                this.submit();
            }
        });

        // 输入时实时验证
        usernameInput.addEventListener('input', function() {
            if (this.value.trim() !== '') {
                usernameError.style.display = 'none';
            }
        });

        passwordInput.addEventListener('input', function() {
            if (this.value.trim() !== '' && this.value.length >= 6) {
                passwordError.style.display = 'none';
            }
        });
    });
</script>
</body>
</html>